package com.ruoyi.cms.controller;

import com.ruoyi.cms.domain.CmsGoods;
import com.ruoyi.cms.service.ICmsGoodsService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;

/**
 * 商品管理Controller
 *
 * @author houyong
 * @date 2025-11-20
 */
@Tag(name = "商品管理", description = "CMS商品管理接口")
@RestController
@RequestMapping("cms/manager/goods")
public class CmsGoodsController extends BaseController {
    @Autowired
    private ICmsGoodsService cmsGoodsService;

    /**
     * 查询商品管理列表
     */
//    @RequiresPermissions("cms:goods:list")
    @Operation(summary = "查询商品管理列表", description = "分页查询商品管理信息列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = TableDataInfo.class)))
    })
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(description = "商品管理查询参数") CmsGoods cmsGoods) {
        startPage();
        List<CmsGoods> list = cmsGoodsService.selectCmsGoodsList(cmsGoods);
        return getDataTable(list);
    }

    /**
     * 获取商品管理详细信息
     */
//    @RequiresPermissions("cms:goods:query")
    @Operation(summary = "获取商品管理详情", description = "根据商品ID获取商品管理详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = AjaxResult.class)))
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@Parameter(description = "商品ID", required = true) @PathVariable("id") Long id) {
        return success(cmsGoodsService.selectCmsGoodsById(id));
    }


    @Operation(summary = "获取商品管理详情", description = "根据商品ID获取商品管理详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = AjaxResult.class)))
    })
    @GetMapping(value = "/detail/{id}")
    public CmsGoods getGoodsInfo(@Parameter(description = "商品ID", required = true) @PathVariable("id") Long id) {
        return cmsGoodsService.selectCmsGoodsById(id);
    }

    /**
     * 新增商品管理
     */
//    @RequiresPermissions("cms:goods:add")
    @Operation(summary = "新增商品管理", description = "新增商品管理信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = AjaxResult.class)))
    })
    @Log(title = "新增商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Parameter(description = "商品管理对象", required = true) @RequestBody CmsGoods cmsGoods) {

        cmsGoods.setCreateTime(DateUtils.getNowDate());
        cmsGoods.setCreateBy(getUsername());
        return toAjax(cmsGoodsService.insertCmsGoods(cmsGoods));
    }

    /**
     * 修改商品管理
     */
//    @RequiresPermissions("cms:goods:edit")
    @Operation(summary = "修改商品管理", description = "修改商品管理信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = AjaxResult.class)))
    })
    @Log(title = "编辑商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Parameter(description = "商品管理对象", required = true) @RequestBody CmsGoods cmsGoods) {

        cmsGoods.setUpdateTime(DateUtils.getNowDate());
        cmsGoods.setUpdateBy(getUsername());
        return toAjax(cmsGoodsService.updateCmsGoods(cmsGoods));
    }

    /**
     * 删除商品管理
     */
//    @RequiresPermissions("cms:goods:remove")
    @Operation(summary = "删除商品管理", description = "批量删除商品管理信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = AjaxResult.class)))
    })
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(description = "商品ID数组", required = true) @PathVariable Long[] ids) {
        return toAjax(cmsGoodsService.deleteCmsGoodsByIds(ids));
    }
}